package com.renwei.yunlong.activity.work;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.CommonSearchActivity;
import com.renwei.yunlong.adapter.PersonSelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.SponsorBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SearchImage;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择发起人--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class PersonSelectActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    public static final int FROM_LAUNCH = 100; // 发起人
    public static final int FROM_COMPLAIN_DEAL = 103; // 投诉      建议 选择服务台
    // 使用统一接口
    public static final int FROM_DEAL = 101; // 处理人 使用默认的头像
    public static final int FROM_COMPLAIN_ASSIGN = 102; // 投诉  指派 选择处理人  没有当前人员信息
    public static final int FROM_PROBLEM_ASSIGN = 104; // 问题  指派 选择处理人
    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    @Bind(R.id.search_image)
    SearchImage searchImage;

    private PersonSelectAdapter adapter;
    private int from;
    private String bindFlag;
    private String ownerCompanyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_sponsor);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        bindFlag = getIntent().getStringExtra("bindFlag");
        from = getIntent().getIntExtra("from", 0);
        ownerCompanyCode = getIntent().getStringExtra("ownerCompanyCode");

        searchImage.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PersonSelectAdapter(this);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);

        if (from == FROM_DEAL) {
            adapter.setShow(true);
        }

        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if (from == FROM_LAUNCH) {
            simpleTileView.setTitle("选择发起人");
            map.put("bindFlag", bindFlag);
            map.put("synCode", ownerCompanyCode);
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
            map.put("name", "");
            ServiceRequestManager.getManager().getWorkSendPerson(this, JsonMapUtil.mapToJson(map), this);
        } else if (from == FROM_COMPLAIN_DEAL) { // 投诉 处理
            simpleTileView.setTitle("选择服务台");
            searchImage.setVisibility(View.GONE);
            String currentUserId = "";
            if ("1".equals(companyType)) {//业主
                currentUserId = ownerBean.getRows().getEmployeeId();
            } else if ("2".equals(companyType)) {//服务商
                currentUserId = serviceLoginBean.getRows().getEmployeeId();
            }
            ServiceRequestManager.getManager().getComplainPerson(this, currentUserId, this);
        } else { // 工单指派选择处理人、投诉指派选择指派人和问题指派选择指派人
            simpleTileView.setTitle("选择处理人");
            if ("1".equals(companyType)) {//业主
                map.put("ownerCode", ownerBean.getRows().getOwnerCode());
                map.put("name", "");
                if (from == FROM_PROBLEM_ASSIGN) {
                    map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                }
            } else if ("2".equals(companyType)) {//服务商
                map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
                map.put("name", "");
                if (from == FROM_PROBLEM_ASSIGN) {
                    map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
                }
            }
            ServiceRequestManager.getManager().getWorkDealPerson(this, JsonMapUtil.mapToJson(map), this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 520) {
                setResult(RESULT_OK, data);
                finish();
            }
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        SponsorBean.Rows bean = adapter.getItem(position);
        Intent intent = new Intent();
        intent.putExtra("name", bean.getName());
        intent.putExtra("employeeId", bean.getEmployeeId());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_image:
                CommonSearchActivity.OpenActivityForPerson(this, v, PersonSelectActivity.class, from, bindFlag, ownerCompanyCode);
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        SponsorBean bean = new Gson().fromJson(result, SponsorBean.class);
        if (bean.getMessage().getCode() == 200) {
            adapter.setData(bean.getRows());
            if (bean.getRows() != null && bean.getRows().size() != 0) {
                stateLayout.showContentView();
            } else {
                stateLayout.showEmptyView();
            }
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }

    public static void OpenActivity(Object context, int from, String bindFlag,
                                    String ownerCompanyCode, int requestCode) {
        if (context instanceof Context) {
            Intent intent = new Intent((Context) context, PersonSelectActivity.class);
            intent.putExtra("from", from);
            intent.putExtra("bindFlag", bindFlag);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else if (context instanceof Fragment) {
            Intent intent = new Intent(((Fragment) context).getContext(), PersonSelectActivity.class);
            intent.putExtra("from", from);
            intent.putExtra("bindFlag", bindFlag);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            ((Fragment) context).startActivityForResult(intent, requestCode);
        }
    }

    public static void OpenActivity(Object context, int from, int requestCode) {
        if (context instanceof Context) {
            Intent intent = new Intent((Context) context, PersonSelectActivity.class);
            intent.putExtra("from", from);
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else if (context instanceof Fragment) {
            Intent intent = new Intent(((Fragment) context).getContext(), PersonSelectActivity.class);
            intent.putExtra("from", from);
            ((Fragment) context).startActivityForResult(intent, requestCode);
        }
    }
}
